/*     */ package com.java.diagnostics.visualizer.impl.sources.implementations;
/*     */ 
/*     */ import com.java.diagnostics.visualizer.exceptions.GCAndMemoryVisualizerException;
/*     */ import com.java.diagnostics.visualizer.impl.sources.SourceImpl;

/*     */ import java.io.BufferedReader;
/*     */ import java.io.IOException;
/*     */ import java.io.InputStream;
/*     */ import java.io.InputStreamReader;
import java.nio.charset.MalformedInputException;
/*     */ //import sun.io.MalformedInputException;
/*     */ 
/*     */ public class StreamSourceImpl extends SourceImpl
/*     */ {
/*     */   private InputStream stream;
/*  22 */   long lastAvailable = 0L;
/*     */ 
/*     */   public StreamSourceImpl(String name, InputStream stream) {
/*  25 */     super(name);
/*  26 */     this.stream = stream;
/*     */   }
/*     */ 
/*     */   public String getText() throws GCAndMemoryVisualizerException
/*     */   {
/*     */     try {
/*  32 */       this.stream.reset();
/*     */ 
/*  34 */       StringBuffer buf = new StringBuffer();
/*  35 */       BufferedReader reader = new BufferedReader(new InputStreamReader(
/*  36 */         this.stream));
/*     */ 
/*  38 */       String txt = reader.readLine();
/*  39 */       while (txt != null) {
/*  40 */         buf.append(txt);
/*  41 */         buf.append(NEWLINE);
/*  42 */         txt = reader.readLine();
/*     */       }
/*     */ 
/*  46 */       this.lastAvailable = this.stream.available();
/*  47 */       return buf.toString();
/*     */     } catch (IOException ioe) {
/*  49 */       throw new GCAndMemoryVisualizerException(ioe);
/*     */     }
/*     */   }
/*     */ 
/*     */   protected InputStream getRawStream() throws GCAndMemoryVisualizerException {
/*     */     try {
/*  55 */       this.stream.reset();
/*  56 */       return this.stream;
/*     */     } catch (IOException ioe) {
/*  58 */       throw new GCAndMemoryVisualizerException(ioe);
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean hasChangedSinceLastGet()
/*     */   {
/*     */     try {
/*  65 */       return this.stream.available() > this.lastAvailable;
/*     */     }
/*     */     catch (IOException localIOException)
/*     */     {
/*     */     }
/*     */ 
/*  73 */     return false;
/*     */   }
/*     */ 
/*     */   public boolean containsWithinReasonableDistanceOfStart(String target) throws GCAndMemoryVisualizerException
/*     */   {
/*     */     try
/*     */     {
/*  80 */       this.lastAvailable = this.stream.available();
/*     */ 
/*  82 */       boolean foundMatch = false;
/*     */ 
/*  84 */       BufferedReader reader = new BufferedReader(new InputStreamReader(
/*  85 */         this.stream));
/*  86 */       int lineCount = 0;
/*  87 */       String line = reader.readLine();
/*  88 */       while ((!foundMatch) && (line != null)) {
/*  89 */         foundMatch = line.indexOf(target) > -1;
/*  90 */         line = reader.readLine();
/*  91 */         lineCount++;
/*     */       }
/*  93 */       this.stream.reset();
/*     */ 
/*  95 */       return foundMatch;
/*     */     }
/*     */     catch (MalformedInputException localMalformedInputException)
/*     */     {
/*  99 */       return false;
/*     */     } catch (IOException ioe) {
/* 101 */       throw new GCAndMemoryVisualizerException(ioe);
/*     */     }
/*     */   }
/*     */ 
/*     */   public boolean containsWithinReasonableDistanceOfStart(String[] targets) throws GCAndMemoryVisualizerException
/*     */   {
/*     */     try {
/* 108 */       this.lastAvailable = this.stream.available();
/* 109 */       boolean foundMatch = false;
/*     */ 
/* 111 */       BufferedReader reader = new BufferedReader(new InputStreamReader(
/* 112 */         this.stream));
/* 113 */       String line = reader.readLine();
/* 114 */       int lineCount = 0;
/* 115 */       while ((!foundMatch) && (line != null)) {
/* 116 */         for (int i = 0; i < targets.length; i++) {
/* 117 */           foundMatch = (foundMatch) || (line.indexOf(targets[i]) > -1);
/*     */         }
/* 119 */         line = reader.readLine();
/* 120 */         lineCount++;
/*     */       }
/*     */ 
/* 123 */       this.stream.reset();
/*     */ 
/* 125 */       return foundMatch;
/*     */     }
/*     */     catch (MalformedInputException localMalformedInputException)
/*     */     {
/* 129 */       return false;
/*     */     } catch (IOException ioe) {
/* 131 */       throw new GCAndMemoryVisualizerException(ioe);
/*     */     }
/*     */   }
/*     */ 
/*     */   public long getApproximateSize()
/*     */     throws GCAndMemoryVisualizerException
/*     */   {
/*     */     try
/*     */     {
/* 142 */       return this.stream.available();
/*     */     } catch (IOException e) {
/* 144 */       throw new GCAndMemoryVisualizerException(e);
/*     */     }
/*     */   }
/*     */ }

